//
//  NEDRequestResponse.m
//  NEDNetwork
//
//  Created by 000 on 2021/12/25.
//

#import "NEDRequestResponse.h"
#import <NEDConstant/NEDConstant+Method.h>

@implementation NEDRequestResponse

+(NSDictionary<NSString *,id> *)modelCustomPropertyMapper{
    return @{
        @"message":@[@"message",@"msg"],
        @"code":@[@"code",@"status"],
        @"result":@[@"result",@"data"]
    };
}

@end

/// json数据类型解析
@interface NSObject(__NEDModelJSonStringParse)<YYModel>

@end

@implementation NSObject(__NEDModelJSonStringParse)

- (NSDictionary *)modelCustomWillTransformFromDictionary:(NSDictionary *)dic{
    NSDictionary<NSString *,id> * mapPropertyClassDic;
    if([self.class respondsToSelector:@selector(modelContainerPropertyGenericClass)]){
        mapPropertyClassDic = [self.class modelContainerPropertyGenericClass];
    }
    if(!mapPropertyClassDic || mapPropertyClassDic.allKeys.count == 0){
        return dic;
    }
    NSMutableDictionary * dictionary = [NSMutableDictionary dictionaryWithDictionary:dic];
    [mapPropertyClassDic enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        if(IsNonNull(dictionary[key])){
            NSString * json = dictionary[key];
            if([json isKindOfClass:[NSString class]]){
                id obj = [NSJSONSerialization JSONObjectWithData:[json dataUsingEncoding:(NSUTF8StringEncoding)] options:(0) error:nil];
                if(obj){
                   if([obj isKindOfClass:[NSDictionary class]] ||
                      [obj isKindOfClass:[NSArray class]]){
                       dictionary[key] = obj;
                   }
                }
            }
        }
    }];
    return dictionary;
}

@end
